package com.best.member.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.best.core.utils.RedisUtil;
import com.best.member.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.best.core.entity.CommonResult;
import com.wf.captcha.ArithmeticCaptcha;

/**
 * @Description 图片验证码控制类
 * @Date 2020/6/18 23:51
 * @Author nike
 **/
@RestController
public class CaptchaController {

    @Autowired
    private RedisUtil redisUtil;
    //登录redis key
    private final static String REDIS_LOGIN_CAPTCHA_KEY = "redis.login.captcha.key.";
    //注册redis key
    private final static String REDIS_REGISTER_CAPTCHA_KEY = "redis.register.captcha.key.";

    /**
     * @Description 登录图片验证码
     * @Date 2020/6/18 23:53
     * @return CommonResult
     * @Author ld
     **/
    @RequestMapping("/login/captcha")
    public CommonResult loginCaptcha() {
        //获取验证码
        Map<String, Object> captchaMap = CaptchaUtil.getArithmetic();
        //将验证码存入redis中,保存时效5分钟
        String redisKey = REDIS_LOGIN_CAPTCHA_KEY + UUID.randomUUID().toString();
        String verCode = (String) captchaMap.get("verCode");
        redisUtil.setString(redisKey,verCode,300L);
        //将redis key 和 base64图片 写入map返回给客户端
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("redisKey",redisKey);
        resMap.put("imgUrl",captchaMap.get("imgUrl"));

        return CommonResult.buildSuccess(resMap);
    }

    /**
     * @Description 注册图片验证码
     * @Date 2020/6/18 23:53
     * @return CommonResult
     * @Author ld
     **/
    @RequestMapping("/register/captcha")
    public CommonResult registerCaptcha() {
        //获取验证码
        Map<String, Object> arithmeticMap = CaptchaUtil.getArithmetic();
        return CommonResult.buildSuccess(arithmeticMap);
    }
}
